package yt.kpool.activity.salesman;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.setting.agent.myguest.MyGuestActivity;
import yt.kpool.adapter.MyGuestAdapter;
import yt.kpool.adapter.SalemanMyGuestAdapter;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.MyGuestBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pop.CurrencyPopupWindow;

public class SalesmanMyGuestActivity extends AppCompatActivity {
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.tv_currrncyname)
    TextView tv_currrncyname;
    @BindView(R.id.iv_curreney)
    ImageView iv_curreney;
    @BindView(R.id.ll_currency)
    LinearLayout ll_currency;
    private List<CurrencyList.DataBean> currencyListData;
    private CurrencyPopupWindow currencyPopupWindow;//币种选择
    private LoadingDialog loadingDialog;
    private int currencyId;//币种id
    private String currencycode = "";//币种code



    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<MyGuestBean.DataBean> dataBeanList;
    private SalemanMyGuestAdapter myGuestAdapter;//我的客户列表

    private int pagenum = 1;//页数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_salesman_my_guest);
        ButterKnife.bind(this);

        initList();

        if (NetWorkUtil.checkNet(SalesmanMyGuestActivity.this)) {
            getCurrrencyData();
        } else {

            ToastUtil.showToast(SalesmanMyGuestActivity.this, getResources().getString(R.string.internet_error));
        }
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({ R.id.ll_currency, R.id.ll_finish})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.ll_finish:

                finish();
                break;
            case R.id.ll_currency:
                //选择币种
                if (currencyPopupWindow != null)
                    currencyPopupWindow.showAsDropDown(ll_currency, 0, 0);
                break;


        }
    }
    /**
     * 获取币种数据
     */
    private void getCurrrencyData() {
        currencyListData = new ArrayList<>();
        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.GetCurrencyList(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "币种列表：" + data);
                    CurrencyList currencyList = MyGson.getInstance().fromJson(data, CurrencyList.class);
                    if (currencyList.getType() == 1) {

                        currencyListData.addAll(currencyList.getData());
                        initCurrencyPopupWindow();
                        if (currencyList.getData().size() > 0) {
                            currencycode = currencyList.getData().get(0).getCode();
                            currencyId = currencyList.getData().get(0).getId();
                            GlideUtil.loadImage(SalesmanMyGuestActivity.this, NetHelperNew.URL + currencyListData.get(0).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                            tv_currrncyname.setText(currencyListData.get(0).getCode().toUpperCase());

                        }
                        getData(pagenum,false);
                    } else {

                        new ErrorDialog(SalesmanMyGuestActivity.this, currencyList.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(SalesmanMyGuestActivity.this, msg);
                }
            });


        }

    }

    /**
     * 币种选择pop初始化
     */
    private void initCurrencyPopupWindow() {
        currencyPopupWindow = new CurrencyPopupWindow(currencyListData, this, new CurrencyPopupWindow.OnSeleCurrencyLayoutListener() {
            @Override
            public void selectHouseLayout(int i) {
                currencyId = currencyListData.get(i).getId();
                currencycode = currencyListData.get(i).getCode();
                GlideUtil.loadImage(SalesmanMyGuestActivity.this, NetHelperNew.URL + currencyListData.get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                tv_currrncyname.setText(currencyListData.get(i).getCode().toUpperCase());

                refreshClear();

            }
        });
    }

    /**
     * 列表初始化
     */
    private void initList() {

        loadingDialog = new LoadingDialog(this, 0);
        dataBeanList = new ArrayList<>();
        myGuestAdapter = new SalemanMyGuestAdapter(this, dataBeanList);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(myGuestAdapter);
        //空白页
        mRecyclerView.setEmptyView(findViewById(R.id.empty));
        findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();

            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.white);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.d_1)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.colorLine)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {

                refreshClear();
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(false);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                getData(pagenum, false);
            }
        });
    }


    private void getData(int pageIndex, final boolean isLoading) {

        if (NetWorkUtil.check(this)) {
            if (isLoading)
                loadingDialog.show();
            NetHelperNew.GuestList(currencyId+"",pageIndex+"", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->","我的客户"+data);
                    if (isLoading)
                        loadingDialog.dismiss();


                    MyGuestBean myGuestBean = MyGson.getInstance().fromJson(data, MyGuestBean.class);


                    if (myGuestBean.getType() == 1) {

                        mRecyclerView.refreshComplete(20);
                        //更新数据
                        dataBeanList.addAll(myGuestBean.getData());

                        myGuestAdapter.notifyDataSetChanged();
                        pagenum++;

                    }else
                    {
                        new ErrorDialog(SalesmanMyGuestActivity.this, myGuestBean.getContent()).show();
                    }



                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    if (isLoading)
                        loadingDialog.dismiss();
                    mRecyclerView.refreshComplete(20);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });
        }

    }

    /**
     * 刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        myGuestAdapter.notifyDataSetChanged();
        pagenum=1;
        getData(pagenum,true);
    }
}
